package com.adayo.memoryfree.adapter;

import android.content.Context;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


import com.adayo.memoryfree.R;
import com.adayo.memoryfree.base.AbsListAdapter;
import com.adayo.memoryfree.bean.AutoStartInfo;
import com.adayo.memoryfree.fragment.AutoStartFragment;
import com.adayo.memoryfree.utils.JToastKit;
import com.adayo.memoryfree.utils.JShellKit;

import java.util.ArrayList;
import java.util.List;

public class AutoStartAdapter extends AbsListAdapter<AutoStartInfo> {

	public static List<Integer> clearIds;
	private Handler mHandler;

	public AutoStartAdapter(Context context, List<AutoStartInfo> apps, Handler mHandler) {
		super(context, apps);
		clearIds = new ArrayList<>();
		this.mHandler = mHandler;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = layoutInflater.inflate(R.layout.listview_auto_start,
					null);
			holder = new ViewHolder();
			holder.appIcon = (ImageView) convertView
					.findViewById(R.id.app_icon);
			holder.appName = (TextView) convertView
					.findViewById(R.id.app_name);
			holder.size = (TextView) convertView
					.findViewById(R.id.app_size);
			holder.disable_switch = (TextView) convertView
					.findViewById(R.id.disable_switch);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		final AutoStartInfo item = (AutoStartInfo) getItem(position);
		if (item != null) {
			holder.appIcon.setImageDrawable(item.getIcon());
			holder.appName.setText(item.getLabel());
			if (item.isEnable()) {
				holder.disable_switch.setBackgroundResource(R.drawable.switch_on);
				holder.disable_switch.setText("已开启");
			} else {
				holder.disable_switch.setBackgroundResource(R.drawable.switch_off);
				holder.disable_switch.setText("已禁止");
			}
			// holder.size.setText(Formatter.formatShortFileSize(mContext, item.getCacheSize()));

			holder.disable_switch.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {

					if (JShellKit.checkRootPermission()) {

						if (item.isEnable()) {
							disableApp(item);
						} else {
							enableApp(item);
						}
					} else {
						JToastKit.getInstance().showMessage("该功能需要获取系统root权限，点击允许获取root权限");
					}
				}
			});
			holder.packageName = item.getPackageName();
		}
		return convertView;
	}

	private void disableApp(AutoStartInfo item) {
		String packageReceiverList[] = item.getPackageReceiver().toString().split(";");

		List<String> mSring = new ArrayList<>();
		for (int j = 0; j < packageReceiverList.length; j++) {
			String cmd = "pm disable " + packageReceiverList[j];
			//部分receiver包含$符号，需要做进一步处理，用"$"替换掉$
			cmd = cmd.replace("$", "\"" + "$" + "\"");
			//执行命令
			mSring.add(cmd);

		}
		JShellKit.CommandResult mCommandResult = JShellKit.execCommand(mSring, true, true);

		if (mCommandResult.result == 0) {
			JToastKit.getInstance().showMessage("已禁止");
			item.setEnable(false);
			notifyDataSetChanged();
			if (mHandler != null) {
				mHandler.sendEmptyMessage(AutoStartFragment.REFRESH_BT);
			}
		} else {
			JToastKit.getInstance().showMessage("禁止失败");
		}
	}

	private void enableApp(AutoStartInfo item) {
		String packageReceiverList[] = item.getPackageReceiver().toString().split(";");

		List<String> mSring = new ArrayList<>();
		for (int j = 0; j < packageReceiverList.length; j++) {
			String cmd = "pm enable " + packageReceiverList[j];
			//部分receiver包含$符号，需要做进一步处理，用"$"替换掉$
			cmd = cmd.replace("$", "\"" + "$" + "\"");
			//执行命令
			mSring.add(cmd);

		}
		JShellKit.CommandResult mCommandResult = JShellKit.execCommand(mSring, true, true);
		if (mCommandResult.result == 0) {
			JToastKit.getInstance().showMessage("已开启");
			item.setEnable(true);
			notifyDataSetChanged();
			if (mHandler != null) {
				mHandler.sendEmptyMessage(AutoStartFragment.REFRESH_BT);
			}
		} else {
			JToastKit.getInstance().showMessage("开启失败");
		}
	}

	class ViewHolder {
		ImageView appIcon;
		TextView appName;
		TextView size;
		TextView disable_switch;
		String packageName;
	}
}
